package com.factory.rental.controller;

import com.factory.rental.common.PageResult;
import com.factory.rental.common.PageSupport;
import com.factory.rental.common.Result;
import com.factory.rental.model.Tenant;
import com.factory.rental.dto.TenantDTO;
import com.factory.rental.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/tenants")
public class TenantController {

    private final TenantService tenantService;

    @Autowired
    public TenantController(TenantService tenantService) {
        this.tenantService = tenantService;
    }

    @PostMapping("save")
    public Result<Tenant> createTenant(@RequestBody TenantDTO tenantDTO) {
        Tenant savedTenant = tenantService.saveTenant(tenantDTO);
        return Result.success(savedTenant, "租户创建成功");
    }

    @GetMapping("/{id}")
    public Result<Tenant> getTenantById(@PathVariable Long id) {
        Optional<Tenant> tenant = tenantService.getTenantById(id);
        if (tenant.isPresent()) {
            return Result.success(tenant.get());
        } else {
            return Result.error(404, "未找到指定ID的租户");
        }
    }

    @GetMapping("/id-card/{idCard}")
    public Result<Tenant> getTenantByIdCard(@PathVariable String idCard) {
        Optional<Tenant> tenant = tenantService.getTenantByIdCard(idCard);
        if (tenant.isPresent()) {
            return Result.success(tenant.get());
        } else {
            return Result.error(404, "未找到指定身份证号的租户");
        }
    }

    @GetMapping("/phone/{phone}")
    public Result<Tenant> getTenantByPhone(@PathVariable String phone) {
        Optional<Tenant> tenant = tenantService.getTenantByPhone(phone);
        if (tenant.isPresent()) {
            return Result.success(tenant.get());
        } else {
            return Result.error(404, "未找到指定电话号码的租户");
        }
    }

    @GetMapping("/name/{name}")
    public Result<List<Tenant>> getTenantsByName(@PathVariable String name) {
        List<Tenant> tenants = tenantService.getTenantsByName(name);
        return Result.success(tenants);
    }

    @GetMapping("/status/{status}")
    public Result<List<Tenant>> getTenantsByStatus(@PathVariable Tenant.TenantStatus status) {
        List<Tenant> tenants = tenantService.getTenantsByStatus(status);
        return Result.success(tenants);
    }

    @GetMapping("/blacklisted")
    public Result<List<Tenant>> getBlacklistedTenants() {
        List<Tenant> tenants = tenantService.getBlacklistedTenants();
        return Result.success(tenants);
    }

    @GetMapping("pageList")
    public PageResult<Tenant> getAllTenants(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageSupport.ofPage(page, size);
        Page<Tenant> tenants = tenantService.getAllTenants(pageable);
        return PageResult.success(tenants);
    }

    @GetMapping("/search")
    public PageResult<Tenant> searchTenants(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(name = "keyword", required = false) String keyword) {
        Pageable pageable = PageSupport.ofPage(page, size);
        Page<Tenant> tenants = tenantService.getAllTenants(pageable, keyword);
        return PageResult.success(tenants);
    }

    @PutMapping("/{id}")
    public Result<Tenant> updateTenant(@PathVariable Long id, @RequestBody TenantDTO tenantDTO) {
        try {
            Tenant updatedTenant = tenantService.updateTenant(id, tenantDTO);
            return Result.success(updatedTenant, "租户信息更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的租户");
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteTenant(@PathVariable Long id) {
        try {
            tenantService.deleteTenant(id);
            return Result.success(null, "租户删除成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的租户");
        }
    }

    @PutMapping("/{id}/blacklist/{blacklist}")
    public Result<Void> blacklistTenant(@PathVariable Long id, @PathVariable boolean blacklist) {
        try {
            tenantService.blacklistTenant(id, blacklist);
            return Result.success(null, "租户黑名单状态更新成功");
        } catch (RuntimeException e) {
            return Result.error(404, "未找到指定ID的租户");
        }
    }
}